<template>
  <div class="nav-menu">
    <div class="logo">
      <img class="logo-img" src="@/assets/img/systemIcon.png" alt="" />
      <h2 v-show="!isFold" class="title">{{ title }}</h2>
    </div>

    <div class="menu">
      <el-scrollbar>
        <el-menu
          :collapse="isFold"
          text-color="#b7bdc3"
          active-text-color="#fff"
          background-color="#001529"
        >
          <template v-for="item in userMenus" :key="item.id">
            <el-sub-menu v-if="hasChildren(item)" :index="item.path">
              <template #title>
                <el-icon>
                  <component :is="item.icon"></component>
                </el-icon>
                <span>{{ item.menuName }}</span>
              </template>
              <template v-for="child in item.children" :key="child.id">
                <el-menu-item :index="child.path" @click="handleRouter(child)">{{
                  child.menuName
                }}</el-menu-item>
              </template>
            </el-sub-menu>
            <el-menu-item v-else :index="item.path" @click="handleRouter(item)">
              <el-icon>
                <component :is="item.icon"></component>
              </el-icon>
              <span>{{ item.menuName }}</span>
            </el-menu-item>
          </template>
        </el-menu>
      </el-scrollbar>
    </div>
  </div>
</template>

<script setup lang="ts">
//在<script setup lang="ts">标签内defineProps不用导入
import { ref } from 'vue'
import useRoutesStore from '@/stores/routes'
import router from '@/router'
const title = ref<string>(import.meta.env.VITE_SYSTEM_TITLE)
//内部用到props才定义
const props = defineProps({
  isFold: {
    type: Boolean,
    default: false
  }
})
console.log(props)
const routesStore = useRoutesStore()
const userMenus = routesStore.menuList

const hasChildren = (menu: any) => {
  if (menu.children && menu.children.length > 0) {
    return true
  } else {
    return false
  }
}
const handleRouter = (menu: any) => {
  //向tabList中添加数据，已经添加过的就不需要添加了
  //数据结构：{title:'首页',path:'home/index'}
  const HasNode = routesStore.tabList.filter((item: any) => item.path == menu.path)
  if (HasNode == null || HasNode.length == 0) {
    const data = {
      title: menu.menuName,
      path: menu.path
    }
    routesStore.setTabList(data)
  }
  //修改当前唤醒页
  routesStore.setActiveTab(menu.path)

  router.push({ path: '/' + menu.path, replace: true })
}
</script>

<style lang="scss" scoped>
.nav-menu {
  height: 100%;
  background-color: #001529;
}

.menu {
  height: calc(100% - 45px);
}

.logo {
  display: flex;
  height: 25px;
  padding: 12px 10px 8px 10px;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  overflow: hidden;

  .logo-img {
    height: 100%;
    margin: 0px 30px 0px 10px;
  }
  .title {
    font-size: 16px;
    font-weight: 700;
    color: white;
    white-space: nowrap;
  }
}

.el-menu {
  border-right: none;
  user-select: none;
}

//因为拿的根元素所以不用 :deep() 就能直接选中
.el-sub-menu {
  .el-menu-item {
    padding-left: 50px !important;
    background-color: #0c2135;
  }
  .el-menu-item:hover {
    color: #fff;
  }
  .el-menu-item.is-active {
    background-color: #0a60bd;
  }
}
</style>
